home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / utilities / print / tspool2.lha / TSPOOL / SOURCE / spoolit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-04  |  4.7 KB  |  200 lines

  1. /* ----------------------------------------------------------------- */
  2. /*     Spoolit Version 1.0 (C) Fourth Level Developments 1994        */
  3. /* ----------------------------------------------------------------- */
  4.  
  5.  
  6. /*
  7.  
  8. TSpool  is  written  by  T.Southwell  and is (C) Fourth Level Developements
  9. 1994.   You  may freely distribute these programs and documentation as long
  10. as you do not make a charge of any more than a reasonable copying fee. If 
  11. you wish to revise or update the software then please do but you must then
  12. send a copy of the new version and source code to Fourth Level Developments.
  13. Fourth Level Developments will be willing to have this software incorporated in other software packages but
  14. in other programmes but you must first put your suggestions to us and obtain
  15. written permission before you start work on it.
  16.  
  17.  
  18. This part of the print spooler reads in the spool files according to a list
  19. which is stored in t:. The files are written one by one to par: and then
  20. deleted.
  21.  
  22. Email: dev@flevel.demon.co.uk
  23.  
  24. */
  25.  
  26. #include <stdio.h>
  27. #define block 64L*1024L
  28. #define uselog 1
  29.  
  30. void* Open();
  31. long Read();
  32. void* AllocMem();
  33. long Seek();
  34. FILE* loghan=0; 
  35. long ll=0;
  36. char logname[160]={"nil:"};
  37.  
  38. main(argc,argv)
  39.  int argc;
  40.  char **argv;
  41.  {
  42.  void* inhan=0;
  43.  void* outhan=0;
  44.  void* listhan=0;
  45.  char workstr[80];
  46.  long size=0;
  47.  char *buffer=0;
  48.  long place=0;
  49.  int f;
  50.  long rlen=0;
  51.  long rnum=0;
  52.  
  53.  
  54.  gimmelog();
  55.  printf("Loghan=%lx\n",loghan);
  56.  fprintf(loghan,"Spoolit starts up.\n"); ll++;
  57.  gimmelog();
  58.  
  59. re_make:
  60.  
  61.     
  62.     while(listhan==0)
  63.          {
  64.          listhan=(void*)Open("t:TSpool-list",1005L);
  65.          if (listhan==0)
  66.             Delay(50L);
  67.          }
  68.  
  69.     Seek(listhan,place,-1L);
  70.     
  71.     rlen=Read(listhan,&rnum,4L);
  72.     if (rlen!=4)
  73.        {
  74.         Close(listhan); listhan=0;
  75.         Delay(50L);
  76.         goto re_make;
  77.        }
  78.        
  79.     place+=4;
  80.     Close(listhan); listhan=0;
  81.  
  82.     fprintf(loghan,"New print file number %ld\n",rnum); ll++;
  83.     gimmelog();
  84.     
  85.     sprintf(workstr,"spool:spool%ld",rnum);
  86.     
  87.    
  88.     inhan=(void*)Open(workstr,(long)1005L);
  89.     if (inhan==0)
  90.        {
  91.        Delay(25L);
  92.        inhan=(void*)Open(workstr,(long)1005L);
  93.        if (inhan==0) 
  94.           {
  95.            gimmelog();
  96.            fprintf(loghan,"Can't open file '%s'\n",workstr); ll++;
  97.            goto re_make;
  98.           }
  99.        }
  100.  
  101.     fprintf(loghan,"Opened file '%s'\n",workstr); ll++;
  102.     
  103.     while (outhan==0)
  104.           {
  105.            gimmelog();
  106.            fprintf(loghan,"Trying to get the parallel port.\n"); ll++;
  107.            outhan=(void*)Open("par:",(long)1006L);
  108.            if (outhan==0)
  109.               Delay(50L);
  110.           }
  111.  
  112.     fprintf(loghan,"Ok, got the parallel port.\n"); ll++;
  113.     size=block;
  114.     
  115.     while (buffer==0) 
  116.           {
  117.            gimmelog();
  118.            fprintf(loghan,"Trying to allocate %ld bytes of memory.\n",size); ll++;
  119.            buffer=(char*)AllocMem(size,1L);
  120.            if (buffer==0)
  121.               Delay(50L);
  122.           }
  123.  
  124.     fprintf(loghan,"Ok, got %ld bytes of memory.\n",size); ll++;
  125.  
  126.     while (size==block)
  127.           {
  128.            gimmelog();
  129.            fprintf(loghan,"Try to read %ld bytes.\n",block); ll++;
  130.            size=Read(inhan,(void*)buffer,block);
  131.            fprintf(loghan,"Have read %ld bytes.\n",size); ll++;
  132.            Write(outhan,(void*)buffer,size);
  133.            fprintf(loghan,"Written %ld bytes to the printer.\n",size); ll++;
  134.           }       
  135.  
  136.     fprintf(loghan,"Shutdown handles.\n"); ll++;
  137.     
  138.     Close(inhan); inhan=0;
  139.     Close(outhan); outhan=0;
  140.     FreeMem(buffer,block); buffer=0;
  141.  
  142.     fprintf(loghan,"Memory now free.\n"); ll++;
  143.  
  144.     sprintf(workstr,"delete spool:spool%ld",rnum);    
  145.     Execute(workstr,0L,0L);
  146.  
  147.     fprintf(loghan,"Deleted file 'spool:spool%ld'\n",rnum); ll++;
  148.     gimmelog();
  149.         
  150.     goto re_make;
  151.  }
  152. gimmelog()
  153. {
  154.  #ifdef uselog
  155.    if ((ll%200)==0)
  156.       {
  157.        printf("Open log a\n");
  158.        if (loghan)
  159.           {
  160.            fclose(loghan);
  161.            loghan=0;
  162.           }
  163.  
  164.        loghan=fopen("t:spoolit.loga","w");
  165.        fclose(loghan); loghan=0;
  166.        loghan=fopen("t:spoolit.loga","a");
  167.        sprintf(logname,"t:spoolit.loga");
  168.        
  169.        fprintf(loghan,"Spoolit opens new log file a.\n");
  170.       }
  171.    else
  172.    if ((ll%200)==100)
  173.       {
  174.        printf("Open log b\n");
  175.        if (loghan) 
  176.           {
  177.            fclose(loghan);
  178.            loghan=0;
  179.           }
  180.        loghan=fopen("t:spoolit.loga","w");
  181.        fclose(loghan); loghan=0;
  182.        loghan=fopen("t:spoolit.loga","a");
  183.        sprintf(logname,"t:spoolit.logb");
  184.  
  185.        fprintf(loghan,"Spoolit opens new log file b.\n"); ll++;
  186.       }
  187.    else
  188.    if (loghan)
  189.       {
  190.        fclose(loghan);
  191.        loghan=fopen(logname,"a");
  192.       }
  193.  #endif
  194.  
  195.  if (loghan==0)
  196.     {
  197.      printf("No loghan is open.\n");
  198.      loghan=fopen("nil:","a");
  199.     }
  200. }